
from copy import copy
from typing import List
from pydantic import BaseModel
from datetime import date, timedelta
from utils.time import Str2Date, Date2Str

def FixDateRange(dateRange, fill : bool):
    dateRange = copy(dateRange)
    if not dateRange and fill:
        dateRange = [Date2Str(date.today() - timedelta(days=6)), Date2Str(date.today())]
    if dateRange and len(dateRange) == 2:
        dateRange[1] = Date2Str(Str2Date(dateRange[1]) + timedelta(hours=23,minutes=59,seconds=59))
    return dateRange

def GetLastCycleDateRange(dateRange):
    dateRange = copy(dateRange)
    if not dateRange:
        dateRange = [Date2Str(date.today() - timedelta(days=6)), Date2Str(date.today())]
    if dateRange and len(dateRange) == 2:
        internalDay = (Str2Date(dateRange[1]) - Str2Date(dateRange[0])).days
        startDate = Date2Str(Str2Date(dateRange[0]) - timedelta(days=internalDay + 1))
        endDate = dateRange[0]
        dateRange[0] = startDate
        dateRange[1] = endDate
    return dateRange

class TaskQuery(BaseModel):
    page: int = 1
    limit: int = 10
    search : str = ''
    id : int = -1
    status : int = -1
    user_id : int = -1
    
class ParkQuery(BaseModel):
    page: int = 1
    limit: int = 10,
    task_id : int = -1
    date_range : List[str] = None
    search : str = ''
    user_id : int = -1

    def preprocess(self):
        self.date_range = FixDateRange(self.date_range, False)
        return self